package com.cqut.forum.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cqut.forum.entity.Article;
import com.cqut.forum.entity.Picture;
import com.cqut.forum.entity.PictureGroup;
import com.cqut.forum.entity.User;

/**
 * 把指定的对象封装为Map形式的键值对
 * @author yangxun
 */
public abstract class PackTo {
	@SuppressWarnings("deprecation")
	public static Map<String, Object> packActive(User user, Article article){
		/*{"image":"images/user_header_left2.jpg","name":"xun",
		    "content":"我饿了","time":"2012-12-12 12:45","reply":[{
			"image":"images/user_header_left2.jpg","name":"xun",
		    "content":"弄东西吃撒！！！","time":"2012-12-12 12:45","reply":null
			}*/
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println(user.getUserPhoto());
		map.put("userId", user.getUserId());
		map.put("articleId", article.getArticleId());
		map.put("image", user.getUserPhoto());
		map.put("name", user.getUserName());
		map.put("content", article.getArticleContents());
		Date d = article.getPublishTime();
		map.put("time", d.toLocaleString());
		map.put("reply", null);
		return map;
	}
	public static Article packArticle(int userId,String content){
		Article article = new Article();
		article.setArticleContents(content);
		article.setArticleGroup(null);
		article.setArticleKeyWords(null);
		article.setArticleState(true);
		article.setIsReview(true);
		article.setModuleId(null);
		article.setPublishTime(new Date());
		article.setLastReplyTime(article.getPublishTime());
		article.setReviewTimes(0);
		article.setVisitTimes(0);
		article.setOwnerId(userId);
		return article;
	}
	public static List<Picture> packToPicture(List<String> url,int userId,PictureGroup pg){
		List<Picture> result = new ArrayList<Picture>();
		for(String s:url){
			Picture p = new Picture();
			p.setIsPublic(pg.getIsPublic());
			p.setIsReview(pg.getIsPublic());
			p.setLastReviewTime(new Date());
			p.setOwnerId(userId);
			p.setPictureAddress(s);
			p.setReviewTimes(0);
			p.setUploadTime(new Date());
			p.setVisitTimes(0);
			p.setPictureGroup(pg);
			p.setPictureState(true);
			result.add(p);
		}
		return result;
	}
	@SuppressWarnings("deprecation")
	public static List<Map<String,Object>> packPictureToMap(List<Map<String, Object>> pictures){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> p:pictures){
			Map<String, Object> m = new HashMap<String, Object>();
			/*m.put("pictureId", p.getPictureId());
			m.put("image", p.getPictureAddress());
			m.put("publishTime", p.getUploadTime().toLocaleString());
			m.put("replyTime", p.getReviewTimes());
			m.put("visitTime", p.getVisitTimes());*/
			m.put("pictureId", p.get("picture_id"));
			m.put("pictureSrc", p.get("picture_address"));
			m.put("publishTime", ((Date)p.get("upload_time")).toLocaleString());
			m.put("replyTime", p.get("review_times"));
			m.put("visitTime", p.get("visit_times"));
			m.put("height", 200);
			result.add(m);
		}
		
		return result;
	}
}
